package com.qiu.kuimosynapse.manager;//package com.qiu.kuimosynapse.manager;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.io.IORuntimeException;
//import cn.hutool.core.util.NumberUtil;
//import cn.hutool.core.util.RandomUtil;
//import com.qcloud.cos.model.PutObjectResult;
//import com.qcloud.cos.model.ciModel.persistence.ImageInfo;
//import com.qiu.kuimosynapse.config.CosClientConfig;
//import com.qiu.kuimosynapse.exception.BusinessException;
//import com.qiu.kuimosynapse.exception.ErrorCode;
//import com.qiu.kuimosynapse.exception.ThrowUtils;
//import com.qiu.kuimosynapse.model.dto.file.UploadPictureResult;
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//import org.apache.commons.lang3.exception.ExceptionUtils;
//
//import javax.annotation.Resource;
//import java.io.File;
//import java.time.LocalDate;
//import java.util.Arrays;
//import java.util.Date;
//import java.util.List;
//
/// **
// * @author <a href="https://github.com/lieeew">leikooo</a>
// */
//@Service
//@Slf4j
//public class FileManager {
//    /**
//     * 1 兆
//     */
//    private static final long ONE_M = 1024 * 1024L;
//
//    private static final List<String> ALLOW_FORMAT_LIST = Arrays.asList("jpeg", "jpg", "png", "webp");
//
//    @Resource
//    private CosClientConfig cosClientConfig;
//
//    @Resource
//    private CosManager cosManager;
//
//
//    public UploadPictureResult uploadPicture2(MultipartFile multipartFile, String uploadPathPrefix) {
//        validPicture(multipartFile);
//        // 图片上传地址
//        String imagePath = generateImageUploadPath(multipartFile, uploadPathPrefix);
//        try {
//            File uploadFile = File.createTempFile(imagePath, null);
//            multipartFile.transferTo(uploadFile);
//            return analyzeCosReturn(new AnalyzeCosParams(cosManager.putPictureObject(imagePath, uploadFile), FileUtil.mainName(multipartFile.getOriginalFilename()), imagePath));
//        } catch (Exception e) {
//            log.error("FileManager#uploadPicture2 error {}", ExceptionUtils.getRootCauseMessage(e));
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传图片失败");
//        } finally {
//            try {
//                FileUtil.del(imagePath);
//            } catch (IORuntimeException e) {
//                log.error("FileManager#uploadPicture2 del filePath {}, error {}", imagePath, ExceptionUtils.getRootCauseMessage(e));
//            }
//        }
//    }
//
//    private UploadPictureResult analyzeCosReturn(AnalyzeCosParams analyzeCosParams) {
//        ImageInfo imageInfo = analyzeCosParams.getPutObjectResult().getCiUploadResult().getOriginalInfo().getImageInfo();
//        return UploadPictureResult.builder()
//                .picFormat(imageInfo.getFormat())
//                .picHeight(imageInfo.getHeight())
//                .picWidth(imageInfo.getHeight())
//                .picSize((long) imageInfo.getQuality())
//                .picScale(NumberUtil.round(imageInfo.getHeight() * 1.0 / imageInfo.getWidth(), 2).doubleValue())
//                .picName(analyzeCosParams.getImageName())
//                .url(String.format("%s/%s", cosManager.getBaseUrl(), analyzeCosParams.getImagePath()))
//                .build();
//
//    }
//
//    private String generateImageUploadPath(MultipartFile multipartFile, String uploadPathPrefix) {
//        String originalFilename = multipartFile.getOriginalFilename();
//        String uploadPath = String.format("%s_%s.%s", LocalDate.now(), RandomUtil.randomString(16), originalFilename);
//        return String.format("%s/%s", uploadPathPrefix, uploadPath);
//    }
//
//    /**
//     * 校验文件
//     *
//     * @param multipartFile multipart 文件
//     */
//    public void validPicture(MultipartFile multipartFile) {
//        ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR, "文件不能为空");
//        // 1. 校验文件大小
//        long fileSize = multipartFile.getSize();
//        ThrowUtils.throwIf(fileSize > 2 * ONE_M, ErrorCode.PARAMS_ERROR, "文件大小不能超过 2M");
//        // 2. 校验文件后缀
//        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
//        // 允许上传的文件后缀
//        ThrowUtils.throwIf(!ALLOW_FORMAT_LIST.contains(fileSuffix), ErrorCode.PARAMS_ERROR, "文件类型错误");
//    }
//
//    /**
//     * 删除临时文件
//     */
//    public void deleteTempFile(File file) {
//        if (file == null) {
//            return;
//        }
//        // 删除临时文件
//        boolean deleteResult = file.delete();
//        if (!deleteResult) {
//            log.error("file delete error, filepath = {}", file.getAbsolutePath());
//        }
//    }
//}
//
/// **
// * 不用成员变量因为多线程时会出问题
// */
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//class AnalyzeCosParams {
//    private PutObjectResult putObjectResult;
//    private String imageName;
//    private String imagePath;
//}
//


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.ImageInfo;
import com.qiu.kuimosynapse.config.CosClientConfig;
import com.qiu.kuimosynapse.exception.BusinessException;
import com.qiu.kuimosynapse.exception.ErrorCode;
import com.qiu.kuimosynapse.exception.ThrowUtils;
import com.qiu.kuimosynapse.manager.upload.FilePictureUpload;
import com.qiu.kuimosynapse.manager.upload.PictureUploadTemplate;
import com.qiu.kuimosynapse.manager.upload.UrlPictureUpload;
import com.qiu.kuimosynapse.model.dto.file.UploadPictureResult;
import com.qiu.kuimosynapse.model.dto.picture.PictureUploadRequest;
import com.qiu.kuimosynapse.model.entity.User;
import com.qiu.kuimosynapse.model.vo.picture.PictureVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 文件管理
 *
 * @author <a href="URL_ADDRESS * @author <a href="https://github.com/lieeew">leikooo</a>
 * @Deprecated 已废弃，改为使用 upload 包的模板方法优化
 */
@Component
@Slf4j
@Deprecated
public class FileManager {
    /**
     * 上传图片
     *
     * @param multipartFile    文件
     * @param uploadPathPrefix 上传路径前缀
     * @return
     */
    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private CosManager cosManager;

    /**
     * 文件服务
     * @deprecated 已废弃，改为使用 upload 包的模板方法优化
     */
    @Deprecated
    public UploadPictureResult uploadPicture(MultipartFile multipartFile, String uploadPathPrefix) {
        // 校验图片
        validPicture(multipartFile);
        // 图片上传地址
        String uuid = RandomUtil.randomString(16);
        String originFilename = multipartFile.getOriginalFilename();
        String uploadFilename = String.format("%s_%s.%s", DateUtil.formatDate(new Date()), uuid,
                FileUtil.getSuffix(originFilename));
        // 上传路劲
        String uploadPath = String.format("/%s/%s", uploadPathPrefix, uploadFilename);
        File file = null;
        try {
            // 创建临时文件
            file = File.createTempFile(uploadPath, null);
            multipartFile.transferTo(file);
            // 上传图片
            PutObjectResult putObjectResult = cosManager.putPictureObject(uploadPath, file);
            ImageInfo imageInfo = putObjectResult.getCiUploadResult().getOriginalInfo().getImageInfo();
            // 封装返回结果
            UploadPictureResult uploadPictureResult = new UploadPictureResult();
            int picWidth = imageInfo.getWidth();
            int picHeight = imageInfo.getHeight();
            double picScale = NumberUtil.round(picWidth * 1.0 / picHeight, 2).doubleValue();
            uploadPictureResult.setPicName(FileUtil.mainName(originFilename));
            uploadPictureResult.setPicWidth(picWidth);
            uploadPictureResult.setPicHeight(picHeight);
            uploadPictureResult.setPicScale(picScale);
            uploadPictureResult.setPicFormat(imageInfo.getFormat());
            uploadPictureResult.setPicSize(FileUtil.size(file));
            // 图片访问路径
            uploadPictureResult.setUrl(cosClientConfig.getHost() + "/" + uploadPath);
            return uploadPictureResult;
        } catch (Exception e) {
            log.error("图片上传到对象存储失败", e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
        } finally {
            this.deleteTempFile(file);
        }
    }

    /**
     * 校验文件
     *
     * @param multipartFile multipart 文件
     */
    public void validPicture(MultipartFile multipartFile) {
        ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR, "文件不能为空");
        // 1. 校验文件大小
        long fileSize = multipartFile.getSize();
        final long ONE_M = 1024 * 1024L;
        ThrowUtils.throwIf(fileSize > 2 * ONE_M, ErrorCode.PARAMS_ERROR, "文件大小不能超过 2M");
        // 2. 校验文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        // 允许上传的文件后缀
        final List<String> ALLOW_FORMAT_LIST = Arrays.asList("jpeg", "jpg", "png", "webp");
        ThrowUtils.throwIf(!ALLOW_FORMAT_LIST.contains(fileSuffix), ErrorCode.PARAMS_ERROR, "文件类型错误");
    }

    /**
     * 删除临时文件
     */
    public void deleteTempFile(File file) {
        if (file == null) {
            return;
        }
        // 删除临时文件
        boolean deleteResult = file.delete();
        if (!deleteResult) {
            log.error("file delete error, filepath = {}", file.getAbsolutePath());
        }
    }

}